package com.jintoufs.util;


import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.request.UploadFileRequest;
import com.qcloud.cos.sign.Credentials;

public class CosUtil {
    private long appId;
    private String secretId;
    private String secretKey;
    private String bucketName;
    private static CosUtil cosUtil = null;
    private static COSClient cosClient = null;
    private void createCos(){
        if (cosClient==null){
//            long appId = 1252508163;
//            String secretId = "AKIDITUJwNbjeXKwNcIrtjPzOiqnZ9ztRAVh";
//            String secretKey = "CC0c2GIAqUxmY8XklZF00uhIg5LDtNi2";
            // 初始化秘钥信息
            Credentials cred = new Credentials(appId, secretId, secretKey);
            // 初始化客户端配置
            ClientConfig clientConfig = new ClientConfig();
            // 设置bucket所在的区域，比如华南园区：gz； 华北园区：tj；华东园区：sh ；
            clientConfig.setRegion("cd");
            // 初始化cosClient
            cosClient = new COSClient(clientConfig, cred);
        }
    }

    public String uploadToCos(UploadFileRequest request){
        request.setBucketName(bucketName);
        if (cosClient==null){
            createCos();
        }
        return cosClient.uploadFile(request);
    }
    public void setAppId(long appId) {
        this.appId = appId;
    }

    public void setSecretId(String secretId) {
        this.secretId = secretId;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }
    /**
     * 工厂方法
     * @return
     */
    public static CosUtil getInstance() {

        if(null==cosUtil){
            cosUtil = new CosUtil();
        }
        return cosUtil;
    }
}
